package com.shp.listener;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * Description:
 * 获取SpringIOC容器的引用
 * @author zwl
 * @version 1.0
 * @date 2020/3/19 16:26
 */
public class MyServletContextListener implements ServletContextListener {

    private static WebApplicationContext ioc = null;

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ioc = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
    }

    /**
     * 获取Spring容器
     * @return WebApplicationContext
     */
    public static WebApplicationContext getIoc(){
        return ioc;
    }
}
